#include "trayicon.h"
#include "core/controller.h"
#include "ukuiwindowhelper/ukuiwindowhelper.h"
#include "ukuistylehelper/ukuistylehelper.h"
TrayIcon::TrayIcon(QObject *parent): QSystemTrayIcon(parent)
{
    m_styleSettings = new QGSettings("org.ukui.style");
    //托盘菜单项 设置 意见反馈 关于 帮助 退出
    QAction *settingAction = new QAction(tr("Setting"), this);
    QAction *aboutAction = new QAction(tr("About"), this);
    QAction *helpAction = new QAction(tr("Help"), this);
    QAction *quitAction = new QAction(tr("Quit"), this);
    QAction *adviceAction = new QAction(tr("Advice"), this);
    adviceAction->setVisible(false);

    settingAction->setObjectName("Kylin-screencast_QAction_setting");
    aboutAction->setObjectName("Kylin-screencast_QAction_about");
    helpAction->setObjectName("Kylin-screencast_QAction_help");
    quitAction->setObjectName("Kylin-screencast_QAction_quit");
    adviceAction->setObjectName("Kylin-screencast_QAction_advice");

    connect(settingAction, &QAction::triggered, this,&TrayIcon::slotOpenSettingWid);
    connect(aboutAction, &QAction::triggered, this,&TrayIcon::slotOpenAboutWid);
    connect(adviceAction, &QAction::triggered, this,&TrayIcon::slotOpenAdviceWid);
    connect(helpAction, &QAction::triggered, this,&TrayIcon::slotOpenHelpWid);
    connect(quitAction, &QAction::triggered, this,&TrayIcon::slotQuitApp);
    connect(quitAction, &QAction::triggered, qApp,&QApplication::quit);


    QMenu *trayIconMenu = new QMenu();
    trayIconMenu->addAction(settingAction);
    trayIconMenu->addAction(adviceAction);
    trayIconMenu->addAction(aboutAction);
    trayIconMenu->addAction(helpAction);
    trayIconMenu->addAction(quitAction);

    this->setToolTip(tr("Screencap"));
    this->setContextMenu(trayIconMenu);

    connect(m_styleSettings,&QGSettings::changed,this,[=](const QString &key) {
        if (key == "styleName") {
            changeStyle();
       }
    });
    changeStyle();
}

void TrayIcon::changeStyle()
{
    QString nowThemeStyle = m_styleSettings->get("styleName").toString();
    if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle || "ukui-default" == nowThemeStyle) {
        this->setIcon(QIcon(":/icon/screencap1.svg"));
    } else {
        this->setIcon(QIcon(":/icon/screencap2.svg"));
    }
}

void TrayIcon::slotOpenSettingWid()
{
    //数据埋点 - 开启设置界面
//    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
//    kdk_dia_upload_default(node, "open-setting-window", "TrayIcon");
//    kdk_dia_data_free(node);

    qDebug()<<"打开设置";
    Controller::getInstance()->openSettingWid();
}

void TrayIcon::slotOpenAboutWid()
{
    //数据埋点 - 开启关于界面
//    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
//    kdk_dia_upload_default(node, "open-about-window", "TrayIcon");
//    kdk_dia_data_free(node);

    qDebug()<<"打开关于";
    QString appVersion = getScreencastVersion();
    m_aboutDialog = new KAboutDialog();
    m_aboutDialog->setWindowTitle(tr("Screencap"));

    if (m_info.isWlcom())
    {
        UkuiWindowHelper *windowHelper = new UkuiWindowHelper(m_aboutDialog);
        windowHelper->removeTitleBar();
    }
    else
    {
        kdk::UkuiStyleHelper::self()->removeHeader(m_aboutDialog);
    }

    //WindowManager::setSkipTaskBar(m_aboutDialog->windowHandle(),false);
    m_aboutDialog->setAppIcon(QIcon::fromTheme("kylin-screencap"));
    m_aboutDialog->setAppName(tr("Screencap"));
    m_aboutDialog->setAppVersion(tr("Version: ").append(appVersion));
    m_aboutDialog->show();
    m_aboutDialog->exec();
}

void TrayIcon::slotOpenHelpWid()
{
    //数据埋点 - 开启帮助手册
//    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
//    kdk_dia_upload_default(node, "open-help-window", "TrayIcon");
//    kdk_dia_data_free(node);

    qDebug()<<"打开帮助";
    if(!ipcDbus)
    {
        ipcDbus = new DaemonIpcDbus();
    }
    if(!ipcDbus->daemonIsNotRunning())
    {
        ipcDbus->showGuide("kylin-screencap");
    }
}

void TrayIcon::slotQuitApp()
{
    //数据埋点 - 退出 app
//    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
//    kdk_dia_upload_default(node, "exit-APP", "TrayIcon");
//    kdk_dia_data_free(node);

    qDebug()<<"点击退出";
    emit sigTrayCloseRec();
    QFile file("/tmp/screenrecord.pid");
    if (file.exists()) {
        file.remove();
    }
}

void TrayIcon::slotOpenAdviceWid()
{

}

QString TrayIcon::getScreencastVersion()
{
    FILE *pp = NULL;
       char *line = NULL;
       size_t len = 0;
       ssize_t read;
       char *q = NULL;
       QString version = tr("none");

       pp = popen("dpkg -l kylin-screencap", "r");
       if(NULL == pp)
           return version;

       while((read = getline(&line, &len, pp)) != -1){
           q = strrchr(line, '\n');
           *q = '\0';

           QString content = line;
           QStringList list = content.split(" ");

           list.removeAll("");

           if (list.size() >= 3)
               version = list.at(2);
       }

       free(line);
       pclose(pp);
       return version;
}
